<?php

/**
 * uniBoard API Style
 *
 * Light and fast forum solution for web 2.0-like communities.
 *
 * @package		uniBoard
 * @author		Rafał Pitoń
 * @license		http://www.gnu.org/licenses/gpl-3.0.html
 * @since		Version 0.1
 * 
 * ------------------------------------------------------------------------
 * 
 * $Date: 2010-09-08 03:38:35 +0000 (Wed, 08 Sep 2010) $
 * $Revision: 60 $
 * $Author: rafio.xudb@gmail.com $
 */

// ------------------------------------------------------------------------

class api_style extends class_api{
	
	/**
	 * Main class pointer
	 *
	 * @var object
	 */
	
	private $appCore			= NULL;
	
	/**
	 * Style ID
	 *
	 * @var integer
	 */
	
	public $style_id 			= 0;
	
	/**
	 * Style Parent
	 *
	 * @var integer
	 */
	
	public $style_parent 		= 0;
	
	/**
	 * Style Imageset
	 *
	 * @var integer
	 */
	
	public $imageset_id		= 0;
	
	/**
	 * Style Name
	 *
	 * @var string
	 */
	
	public $style_name			= '';
	
	/**
	 * Style Author
	 *
	 * @var string
	 */
	
	public $style_author 		= '';
	
	/**
	 * Style E-Mail
	 *
	 * @var string
	 */
	
	public $style_email 		= '';
	
	/**
	 * Style WWW
	 *
	 * @var string
	 */
	
	public $style_www	 		= '';
	
	/**
	 * Style CSS
	 *
	 * @var string
	 */
	
	public $style_css	 		= '';
	
	/**
	 * Style JavaScript
	 *
	 * @var string
	 */
	
	public $style_js	 		= '';
	
	// ------------------------------------------------------------------------

	/**
	 * Initialise Api
	 *
	 * @param object $appCore
	 * @param integer $target_id
	 */
	
	function __construct( $appCore, $target_id = 0){
					
		// Store appCore pointer
		$this -> appCore = $appCore;
	
		// Store style id
		$this -> style_id 	= (integer) $target_id;
		
		// Autoload If Style ID is bigger than zero
		if ( $target_id > 0)
		{
			// Make select
			$appCore -> db -> query( 'SELECT *
			FROM ' . DB_PREFIX . 'styles
			WHERE style_id = \'' . $this -> style_id . '\'
			LIMIT 1');
			
			// Style found
			if ( $style_result = $appCore -> db -> fetch_assoc())
			{
				$this -> data_loaded			= true;
				
				$this -> style_parent 			= $style_result['style_parent'];
				$this -> imageset_id			= $style_result['imageset_id'];
				$this -> style_name 			= $style_result['style_name'];
				$this -> style_author 			= $style_result['style_author'];
				$this -> style_email 			= $style_result['style_email'];
				$this -> style_www 				= $style_result['style_www'];
				$this -> style_css 				= $style_result['style_css'];
				$this -> style_js 				= $style_result['style_js'];
			}
			else
			{
				//give action an error
				$this -> setError( $appCore -> lang['acp_style_not_found']);
			}
			
			// Free result
			$appCore -> db -> freeResult();
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Save API data to DB
	 *
	 */
	
	function saveData(){
		
		// Do mysql query only when we have changes, and no errors
		if ( !empty( $this -> changes) && !$this -> error)
		{	
			// Insert?
			if ( $this -> style_id == 0)
			{
				// Insert new record
				$this -> appCore -> db -> insert( $this -> changes, 'styles');
			
				// Store new style id
				$this -> style_id = $this -> appCore -> db -> insertId();
			}
			else
			{
				// Update record
				$this -> appCore -> db -> update( $this -> changes, 'styles', 'style_id = \'' . $this -> style_id . '\'');
			}
		
			// Flush cache
			class_cache::flushCache( 'styles');
			
			// AppCore cache too?
			if ( isset( $this -> appCore -> cache_store['styles']))
			{
				unset( $this -> appCore -> cache_store['styles']);
			}
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set style_parent
	 *
	 * @param integer $new_value
	 */
	
	function setStyleParent( $new_value){
		
		// Grab avaiable styles
		$avaiable_parents = $this -> appCore -> getStyles( true, $this -> style_id);
		
		// Set type
		settype( $new_value, 'integer');
		
		// Parent Style Exists?
		if ( isset( $avaiable_parents[$new_value]))
		{
			// Store in object
			$this -> style_parent = $new_value;
			
			// Add to made changes
			$this -> changes['style_parent'] = $this -> style_parent;
		}
		else
		{
			// Set error
			$this -> setError( $this -> appCore -> lang['acp_style_parent_wrong']);
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set style_imageset
	 *
	 * @param integer $new_value
	 */
	
	function setImagesetId( $new_value){
		
		// Store in object
		$this -> imageset_id = (integer) $new_value;
		
		// Add to made changes
		$this -> changes['imageset_id'] = $this -> imageset_id;
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set style_name
	 *
	 * @param integer $new_value
	 */
	
	function setStyleName( $new_value){
		
		if ( isset( $new_value[0]))
		{
			// Store in object
			$this -> style_name = $new_value;
			
			// Check max lenght
			if ( mb_strlen( $new_value) > 255)
			{
				// Set error
				$this -> setError( $this -> appCore -> lang['acp_style_name_too_long']);
			}
			else
			{
				// Add to made changes
				$this -> changes['style_name'] = $this -> appCore -> db -> strEscape( $this -> style_name);
			}
		}
		else
		{
			// Set error
			$this -> setError( $this -> appCore -> lang['acp_style_name_empty']);
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set style_author
	 *
	 * @param integer $new_value
	 */
	
	function setStyleAuthor( $new_value){
		
		// Store in object
		$this -> style_author = $new_value;

		// Check lenght
		if ( mb_strlen( $new_value) > 255)
		{
			// Set Error
			$this -> setError( $this -> appCore -> lang['acp_style_author_too_long']);
		}
		else
		{
			// Add to made changes
			$this -> changes['style_author'] = $this -> appCore -> db -> strEscape( $this -> style_author);
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set style_email
	 *
	 * @param integer $new_value
	 */
	
	function setStyleEmail( $new_value){

		// Store in object
		$this -> style_email = $new_value;
	
		// Check lenght
		if ( mb_strlen( $new_value) > 255)
		{
			// Set Error
			$this -> setError( $this -> appCore -> lang['acp_style_email_too_long']);
		}
		else
		{
			// Add to made changes
			$this -> changes['style_email'] = $this -> appCore -> db -> strEscape( $this -> style_email);
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set style_www
	 *
	 * @param integer $new_value
	 */
	
	function setStyleWww( $new_value){

		// Store in object
		$this -> style_www = $new_value;
	
		// Check lenght
		if ( mb_strlen( $new_value) > 255)
		{
			// Set Error
			$this -> setError( $this -> appCore -> lang['acp_style_www_too_long']);
		}
		else
		{
			// Add to made changes
			$this -> changes['style_www'] = $this -> appCore -> db -> strEscape( $this -> style_www);
		}
		
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set style_css
	 *
	 * @param integer $new_value
	 */
	
	function setStyleCss( $new_value){

		// Style empty?
		if ( isset( $new_value[0]))
		{
			// Store in object
			$this -> style_css = $new_value;
		
			// Add to made changes
			$this -> changes['style_css'] = $this -> appCore -> db -> strEscape( $this -> style_css);
		}
		else
		{
			// Add error
			$this -> setError( $this -> appCore -> lang['acp_style_css_empty']);
		}
	}
	
	// ------------------------------------------------------------------------

	/**
	 * Set style_js
	 *
	 * @param integer $new_value
	 */
	
	function setStyleJs( $new_value){

		// Store in object
		$this -> style_js = $new_value;
	
		// Add to made changes
		$this -> changes['style_js'] = $this -> appCore -> db -> strEscape( $this -> style_js);
		
	}
	
}